package cn.kgc.changjian.controller;

import cn.kgc.changjian.pojo.Faqs;
import cn.kgc.changjian.pojo.ResultEntity;
import cn.kgc.changjian.service.FaqsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2020-05-25 20:39
 */
@RestController
public class FaqsController {

    @Resource
    FaqsService faqsService;

    @RequestMapping("/pageInfo")
    public ResultEntity<PageInfo<Faqs>> pageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSzie", defaultValue = "3", required = false) Integer pageSize,
            @RequestParam(value = "classId", defaultValue = "0", required = false) Integer classId
    ){
        PageInfo<Faqs> pageInfo = faqsService.selectPageInfo(pageNum, pageSize, classId);
        return ResultEntity.successWithoutData(pageInfo);
    }


    //删除
    @RequestMapping("/delete")
    public ResultEntity<String> delete(Integer id){
        try {
            Integer delete = faqsService.delete(id);
            return ResultEntity.successWithoutData(delete+"");
        } catch (Exception e) {
            return ResultEntity.failed(e.getMessage());
        }
    }

    //根据id查询
    @RequestMapping("/selectById")
    public ResultEntity<Faqs> selectById(Integer id){
        Faqs faqs = faqsService.selectById(id);
        return ResultEntity.successWithoutData(faqs);
    }

    //增加 和修改
    @RequestMapping("/add")
    public ResultEntity<String> add(@RequestBody Faqs faqs){
        try {
            Integer integer=0;

            if(faqs.getId()==null){
               faqsService.add(faqs);
            }else{
                faqsService.update(faqs);
            }
            return ResultEntity.successWithoutData(integer+"");
        } catch (Exception e) {
         return  ResultEntity.failed(e.getMessage());
        }
    }
}
